using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
namespace MicroMsg.Plugin.DriftBottle.Scene
{
	public class DriftBottleServiceCenter
	{
		private const string TAG = "DriftBottleServiceCenter";
		public static NetSceneGetBottleCount sceneGetBottleCount;
		public static NetScenePickBottle scenePickBottle;
		public static NetSceneOpenBottle sceneOpenBottle;
		public static int maxCopyCount = 12;
		private static int lastGetCountDate = 0;
		private static int pickBottleCountLeft = 1;
		private static int throwBottleCountLeft = 1;
		public static void init()
		{
			DriftBottleServiceCenter.sceneGetBottleCount = new NetSceneGetBottleCount();
			DriftBottleServiceCenter.scenePickBottle = new NetScenePickBottle();
			DriftBottleServiceCenter.sceneOpenBottle = new NetSceneOpenBottle();
			EventCenter.registerEventHandler(EventConst.ON_ACCOUNT_LOGIN, new EventHandlerDelegate(DriftBottleServiceCenter.onAccountLoginProc));
		}
		private static void onAccountLoginProc(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			Log.smethod_5("DriftBottleServiceCenter", "switch account clear lastGetCountDate");
			DriftBottleServiceCenter.lastGetCountDate = 0;
		}
		private static void GetBottleCount()
		{
			Log.smethod_4("DriftBottleServiceCenter", "begin GetBottleCount()");
			EventCenter.registerEventHandler(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new EventHandlerDelegate(DriftBottleServiceCenter.onGetBottleCount));
			DriftBottleServiceCenter.sceneGetBottleCount.doScene();
		}
		public static void proccessBottleMsg(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				Log.smethod_1("DriftBottleServiceCenter", "proccessBottleMsg fail cmdAM == null");
				return;
			}
			string pluginNameByChatMsg = ExtentCenter.getPluginNameByChatMsg(chatMsg_0);
			if ("DriftBottle".Equals(pluginNameByChatMsg))
			{
				Log.smethod_4("DriftBottleServiceCenter", "Receive a DriftBottle Msg");
				string[] array = chatMsg_0.strTalker.Split(new char[]
				{
					':'
				});
				if (array == null || array.Length < 2)
				{
					Log.smethod_1("DriftBottleServiceCenter", "strTmpArry is error");
					return;
				}
				string text = array[1];
				if (string.IsNullOrEmpty(text))
				{
					Log.smethod_1("DriftBottleServiceCenter", "bottleId is error");
					return;
				}
				Log.smethod_5("DriftBottleServiceCenter", "bottleId = " + text);
				if (StorageMgr.bContact.method_0(array[0]) == null)
				{
					Log.smethod_5("DriftBottleServiceCenter", "bContact is not exist begin sceneBatchGetContact");
					List<string> list = new List<string>();
					list.Add(array[0]);
					ServiceCenter.sceneBatchGetContact.doScene(list);
				}
				BottleInfo bottleInfo = StorageMgr.bottle.method_0(text);
				if (bottleInfo != null && bottleInfo.nMsgLocalID <= 0)
				{
					ChatMsg chatMsg = new ChatMsg();
					chatMsg.nCreateTime = bottleInfo.nCreateTime;
					Log.smethod_5("DriftBottleServiceCenter", "newMsg.nCreateTime = " + chatMsg.nCreateTime);
					chatMsg.nIsSender = bottleInfo.nIsSender;
					chatMsg.nMsgType = bottleInfo.nMsgType;
					if (bottleInfo.nMsgType == 34)
					{
						string text2 = DriftBottleServiceCenter.copyFile(bottleInfo.strFilePath);
						if (string.IsNullOrEmpty(text2))
						{
							return;
						}
						chatMsg.strPath = text2;
					}
					chatMsg.strClientMsgId = bottleInfo.strParentClientid;
					chatMsg.strMsg = bottleInfo.strContent;
					chatMsg.strTalker = chatMsg_0.strTalker;
					chatMsg.nStatus = 2;
					StorageMgr.chatMsg.addMsg(chatMsg);
					bottleInfo.nMsgLocalID = chatMsg.nMsgLocalID;
					StorageMgr.bottle.modify(bottleInfo);
					return;
				}
				Log.smethod_1("DriftBottleServiceCenter", "Can not find throwBottleInfo bottleId = " + text);
			}
		}
		private static string copyFile(string srcFileName)
		{
			string result;
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.FileExists(srcFileName))
					{
						result = null;
					}
					else
					{
						string hashString = MD5Core.GetHashString(srcFileName + Util.getNowMilliseconds());
						Log.smethod_5("DriftBottleServiceCenter", "srcFileName = " + srcFileName);
						string text = StorageIO.getPluginPath(DriftBottle.getInstance().mName) + "/voice/" + hashString + ".amr";
						Log.smethod_5("DriftBottleServiceCenter", "destFileName = " + text);
						userStoreForApplication.CopyFile(srcFileName, text, true);
						result = text;
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_5("storage", "writeToFile file fail" + ex);
				result = null;
			}
			return result;
		}
		private static void onGetBottleCount(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (evtArgs.mEventID != EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT)
			{
				return;
			}
			EventCenter.removeEventHandler(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new EventHandlerDelegate(DriftBottleServiceCenter.onGetBottleCount));
			if (evtArgs.mObject.GetType() != typeof(BottleCountRet))
			{
				Log.smethod_1("DriftBottleServiceCenter", "BottleCountRet type error");
				return;
			}
			BottleCountRet bottleCountRet = (BottleCountRet)evtArgs.mObject;
			if (bottleCountRet != null && bottleCountRet.bottleEvent_0 == BottleEvent.ON_NETSCENE_GETBOTTLECOUNT_SUCCESS)
			{
				DriftBottleServiceCenter.pickBottleCountLeft = bottleCountRet.pickCount;
				DriftBottleServiceCenter.throwBottleCountLeft = bottleCountRet.throwCount;
				DriftBottleServiceCenter.lastGetCountDate = DriftBottleServiceCenter.getNowDay();
				Log.smethod_4("DriftBottleServiceCenter", "GetBottleCount pickBottleCountLeft = " + DriftBottleServiceCenter.pickBottleCountLeft);
				Log.smethod_4("DriftBottleServiceCenter", "GetBottleCount throwBottleCountLeft = " + DriftBottleServiceCenter.throwBottleCountLeft);
				return;
			}
		}
		public static void doSayHiToBottleContact(string username, string content)
		{
			NetSceneSayHiEx netSceneSayHiEx = new NetSceneSayHiEx();
			netSceneSayHiEx.doSceneEx(username, content, AddContactScene.MM_ADDSCENE_BOTTLE, EventConst.ON_LBS_SAYHI_SUCCESS, EventConst.ON_LBS_SAYHI_ERROR);
		}
		public static bool isPickBottleCountLeft()
		{
			if (!DriftBottleServiceCenter.isTodayHasBeenGetCount())
			{
				DriftBottleServiceCenter.GetBottleCount();
				return true;
			}
			return DriftBottleServiceCenter.pickBottleCountLeft > 0;
		}
		public static bool isThrowBottleCountLeft()
		{
			if (!DriftBottleServiceCenter.isTodayHasBeenGetCount())
			{
				DriftBottleServiceCenter.GetBottleCount();
				return true;
			}
			return DriftBottleServiceCenter.throwBottleCountLeft > 0;
		}
		private static bool isTodayHasBeenGetCount()
		{
			Log.smethod_5("DriftBottleServiceCenter", "lastGetCountDate = " + DriftBottleServiceCenter.lastGetCountDate);
			Log.smethod_5("DriftBottleServiceCenter", "getNowDay() = " + DriftBottleServiceCenter.getNowDay());
			if (DriftBottleServiceCenter.lastGetCountDate != 0)
			{
				if (DriftBottleServiceCenter.lastGetCountDate == DriftBottleServiceCenter.getNowDay())
				{
					return true;
				}
			}
			return false;
		}
		public static int getNowDay()
		{
			DateTime dateTime = DateTime.get_Now().ToUniversalTime();
			DateTime dateTime2 = new DateTime(1970, 1, 1);
			return dateTime.Subtract(dateTime2).get_Days();
		}
		public static void setBottleCount(int pickCount, int throwCount)
		{
			if (pickCount >= 0)
			{
				DriftBottleServiceCenter.setPickBottleLeftCount(pickCount);
			}
			if (throwCount >= 0)
			{
				DriftBottleServiceCenter.setThrowABottleLeftCount(throwCount);
			}
			DriftBottleServiceCenter.lastGetCountDate = DriftBottleServiceCenter.getNowDay();
		}
		private static void setPickBottleLeftCount(int count)
		{
			DriftBottleServiceCenter.pickBottleCountLeft = count;
		}
		private static void setThrowABottleLeftCount(int count)
		{
			DriftBottleServiceCenter.throwBottleCountLeft = count;
		}
		public static bool DelBottle(string toTalker)
		{
			if (string.IsNullOrEmpty(toTalker))
			{
				return false;
			}
			string[] array = toTalker.Split(new char[]
			{
				':'
			});
			if (array != null && array.Length >= 1)
			{
				OpLogMgr.OpDeleteBottle(toTalker, 0);
				ServiceCenter.sceneNewSync.doScene(7, syncScene.MM_NEWSYNC_SCENE_OTHER);
				StorageMgr.bContact.delContact(toTalker);
				BottleInfo bottleInfo = StorageMgr.bottle.method_0(array[1]);
				if (bottleInfo != null)
				{
					if (!string.IsNullOrEmpty(bottleInfo.strFilePath))
					{
						StorageIO.deleteFile(bottleInfo.strFilePath);
					}
					StorageMgr.bottle.method_3(array[1]);
				}
				return true;
			}
			Log.smethod_1("DriftBottleServiceCenter", "DelBottle err usrName toTalker = " + toTalker);
			return false;
		}
		public static void checkCleanThrowBottle()
		{
			bool @bool = ConfigMgr.getBool("isNeedDelThrowBottle", true);
			Log.smethod_5("DriftBottleServiceCenter", "StorageMgr.bottle.count() = " + StorageMgr.bottle.count());
			int num = StorageMgr.bottle.count();
			if (num > 600)
			{
				ConfigMgr.setBool("isNeedDelThrowBottle", true);
				Log.smethod_5("DriftBottleServiceCenter", "ready delete count = " + (DriftBottleServiceCenter.maxCopyCount + 5));
				DriftBottleServiceCenter.delTop(DriftBottleServiceCenter.maxCopyCount + 5);
				return;
			}
			if (num < 600 && num > 300)
			{
				if (@bool)
				{
					Log.smethod_5("DriftBottleServiceCenter", "been 600 ready delete count = " + (DriftBottleServiceCenter.maxCopyCount + 5));
					DriftBottleServiceCenter.delTop(DriftBottleServiceCenter.maxCopyCount + 5);
					return;
				}
			}
			else
			{
				ConfigMgr.setBool("isNeedDelThrowBottle", false);
			}
		}
		private static void delTop(int topNum)
		{
			List<BottleInfo> orderList = StorageMgr.bottle.getOrderList(0, topNum);
			using (List<BottleInfo>.Enumerator enumerator = orderList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					BottleInfo current = enumerator.get_Current();
					if (current != null && !string.IsNullOrEmpty(current.strFilePath))
					{
						using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
						{
							if (userStoreForApplication.FileExists(current.strFilePath))
							{
								userStoreForApplication.DeleteFile(current.strFilePath);
							}
						}
					}
				}
			}
			StorageMgr.bottle.delList(orderList);
		}
		public static void cleanBottlePluginFile()
		{
			string pluginPath = StorageIO.getPluginPath(DriftBottle.getInstance().mName);
			StorageIO.emptyDir(pluginPath);
		}
		public static void checkCleanBottleContact()
		{
		}
		public static void testAddThrowBottle()
		{
		}
	}
}
